﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WadViewer.DomainModel;
using System.Configuration;
using WadViewer.Configuration;

namespace WadViewer.DomainLayer.Test
{
	[TestClass]
	public class TestRetrieveWadTables
	{
		[TestMethod]
		public void CanGetWadTraceLogs()
		{
			var repository = new WadRepository();
			repository.Setup(GetConnection());

			var traceLogs = repository.RetrieveTraceLogs(DateTime.Now.Date.AddDays(-30), DateTime.Now.Date, 5);

			Assert.IsTrue(traceLogs.Count > 0);
			Assert.IsTrue(traceLogs.All(t => t.PartitionKey != null));
			Assert.IsTrue(traceLogs.All(t => t.RowKey != null));
			Assert.IsTrue(traceLogs.All(t => t.Timestamp != DateTime.MinValue));
			Assert.IsTrue(traceLogs.All(t => t.DeploymentId != null));
			Assert.IsTrue(traceLogs.All(t => t.Role != null));
			Assert.IsTrue(traceLogs.All(t => t.RoleInstance != null));
			Assert.IsTrue(traceLogs.All(t => t.Message != null));
		}

		[TestMethod]
		public void CanGetWadEventLogs()
		{
			var repository = new WadRepository();
			repository.Setup(GetConnection());

			var eventLogs = repository.RetrieveEventLogs(DateTime.Now.Date.AddDays(-30), DateTime.Now.Date, 5);

			Assert.IsTrue(eventLogs.Count > 0);
			Assert.IsTrue(eventLogs.All(t => t.PartitionKey != null));
			Assert.IsTrue(eventLogs.All(t => t.RowKey != null));
			Assert.IsTrue(eventLogs.All(t => t.Timestamp != DateTime.MinValue));
			Assert.IsTrue(eventLogs.All(t => t.DeploymentId != null));
			Assert.IsTrue(eventLogs.All(t => t.Role != null));
			Assert.IsTrue(eventLogs.All(t => t.RoleInstance != null));
			Assert.IsTrue(eventLogs.All(t => t.ProviderName != null));
			Assert.IsTrue(eventLogs.All(t => t.Channel != null));
			Assert.IsTrue(eventLogs.All(t => t.Description != null));
			Assert.IsTrue(eventLogs.All(t => t.RawXml != null));
		}

		[TestMethod]
		public void CanGetPerfCounters()
		{
			var repository = new WadRepository();
			repository.Setup(GetConnection());

			var eventLogs = repository.RetrievePerfCounters(DateTime.Now.Date.AddDays(-30), DateTime.Now.Date, 5);

			Assert.IsTrue(eventLogs.Count > 0);
			Assert.IsTrue(eventLogs.All(t => t.PartitionKey != null));
			Assert.IsTrue(eventLogs.All(t => t.RowKey != null));
			Assert.IsTrue(eventLogs.All(t => t.Timestamp != DateTime.MinValue));
			Assert.IsTrue(eventLogs.All(t => t.DeploymentId != null));
			Assert.IsTrue(eventLogs.All(t => t.Role != null));
			Assert.IsTrue(eventLogs.All(t => t.RoleInstance != null));
			Assert.IsTrue(eventLogs.All(t => t.CounterName != null));
			Assert.IsTrue(eventLogs.All(t => t.CounterValue != null));
		}

		public TableConnection GetConnection()
		{
			return ConnectionManager.GetConnections().First();
		}
	}
}
